<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Carbon\Carbon;
use Think\Model\RelationModel;

class AdminsModel extends RelationModel {

    /**
     * 分页查询
     * @param $param
     * @return array
     */
    function getData($param){
        // 请求参数
        if($param['key']){
            $where['a.nickname'] = ['like', '%'.$param['key'].'%'];
            $where['a.username'] = ['like', '%'.$param['key'].'%'];
        }

        $where2 = [];
        if($param['status']){
            $where2['a.status'] = $param['status'];
        }
        if($where && $where2){
            $where['_logic'] = 'or';
            $where2['_complex'] = $where;
        }elseif($where && !$where2){
            $where['_logic'] = 'or';
            $where2 = $where;
        }elseif(!$where && !$where2){
            $where2 = [];
        }

        // 获取数据
        $pageNum = ($param['page'] - 1) * $param['size'];
        $data['count'] = $this->alias('a')
            ->where($where2)->count();
        if($data['count'] < $pageNum){
            return ['status' => false, 'msg'=>'没有更多数据了'];
        }
        $data['data'] = $this->alias('a')
            ->field('a.*, b.name role_name')
            ->join('LEFT JOIN __ADMIN_ROLES__ b ON a.status = b.id')
            ->where($where2)
            ->order('a.created_at desc')
            ->limit($pageNum, $param['size'])
            ->select();

        return ['status'=> true, 'data'=> $data];
    }
}
